package com.kvest.looper_test;

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import com.kvest.looper_test.Looper.LoopThread;

public class LooperTestActivity extends Activity {
    LoopThread loopThread;
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void startLoopThread(View view) {
        loopThread = new LoopThread();
        loopThread.start();
    }

    public void stopLoopThread(View view) {
        if (loopThread != null) {
            loopThread.stop();
            loopThread = null;
        }
    }

    public void workLoopThread(View view) {
        if (loopThread != null) {
            Runnable r = new Runnable() {
                @Override
                public void run() {
                    Log.d("LOOPER_TEST", "tworkLoopThread from Runnable");
                }
            };

            Message m1 = loopThread.getCustomHandler().obtainMessage(1, "Test");
            Message m2 = loopThread.getCustomHandler().obtainMessage(2, new Integer(7));

            loopThread.getCustomHandler().post(r);
            loopThread.getCustomHandler().sendMessage(m1);
            loopThread.getCustomHandler().sendMessage(m2);

            m1 = loopThread.getEmptyHandler().obtainMessage(1, "Test");
            m2 = loopThread.getEmptyHandler().obtainMessage(2, new Integer(7));

            loopThread.getEmptyHandler().post(r);
            loopThread.getEmptyHandler().sendMessage(m1);
            loopThread.getEmptyHandler().sendMessage(m2);

            m1 = loopThread.getEmptyHandler().obtainMessage(1, "Test");
            m2 = loopThread.getEmptyHandler().obtainMessage(2, new Integer(7));

            loopThread.getCallbackHandler().post(r);
            loopThread.getCallbackHandler().sendMessage(m1);
            loopThread.getCallbackHandler().sendMessage(m2);
        }
    }
}
